﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_QuanLyLop_Khoi_Mon : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    SoLienLacDienTu cls = new SoLienLacDienTu();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                LoadGrvKhoi();
                MaTuTangKhoi();
                LoadgrvBan();
                MaTuTangBan();
                cls.LoadddlKhoiHoc(ddlKhoiHoc);
                LoadgrvLop();
                TangMaLop();
                TangMaMon();
                LoadgrvMon();
            }
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        if (txtTenKhoi.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa nhập tên khối');", true);
        }
        else
        {
            KhoiHoc kh = new KhoiHoc();
            kh.MaKhoi = int.Parse(lblMa.Text);
            kh.TenKhoi = txtTenKhoi.Text;
            db.KhoiHocs.InsertOnSubmit(kh);
            db.SubmitChanges();
            LoadGrvKhoi();
            txtTenKhoi.Text = "";
        }
    }
    void LoadGrvKhoi()
    {
        var c = from p in db.KhoiHocs select p;
        grvKhoiHoc.DataSource = c;
        grvKhoiHoc.DataBind();
    }
    void MaTuTangKhoi()
    {
        var c = from p in db.KhoiHocs select p.MaKhoi;
        if (c.Count() == 0)
        {
            lblMa.Text = "1";
        }
        if (c.Count() >= 1)
        {
            int max = 0;
            //string s1 = c.ToString();
            int max1 = 0;
            foreach (var con in c)
            {
                max = con;
            }
            max1 = max + 1;
            lblMa.Text = max1.ToString();
        }
    }
    protected void grvKhoiHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvKhoiHoc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMa");
        KhoiHoc kh = db.KhoiHocs.SingleOrDefault(p=>p.MaKhoi==int.Parse(Ma.Text));
        lblMa.Text = kh.MaKhoi.ToString();
        txtTenKhoi.Text = kh.TenKhoi;
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvKhoiHoc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMa");
        KhoiHoc kh = db.KhoiHocs.SingleOrDefault(p => p.MaKhoi == int.Parse(Ma.Text));
        kh.MaKhoi = int.Parse(Ma.Text);
        kh.TenKhoi = txtTenKhoi.Text;
        db.SubmitChanges();
        LoadGrvKhoi();
        txtTenKhoi.Text = "";
    }
    protected void grvBanHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvBanHoc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaBan");
        BanHoc bn = db.BanHocs.SingleOrDefault(p=>p.MaBan==int.Parse(Ma.Text));
        lblMaBan.Text = bn.MaBan.ToString();
        txtTenBan.Text = bn.TenBan;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtTenBan.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Chưa nhập tên ban');", true);
        }
        else
        {
            BanHoc bn = new BanHoc();
            bn.MaBan = int.Parse(lblMa.Text);
            bn.TenBan = txtTenBan.Text;
            db.BanHocs.InsertOnSubmit(bn);
            db.SubmitChanges();
            LoadgrvBan();
            txtTenBan.Text = "";
        }
    }
    protected void btnUpDate_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvBanHoc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaBan");
        BanHoc bn = db.BanHocs.SingleOrDefault(p => p.MaBan == int.Parse(Ma.Text));
        bn.MaBan = int.Parse(Ma.Text);
        bn.TenBan = txtTenBan.Text;
        db.SubmitChanges();
        LoadgrvBan();
        txtTenBan.Text = "";
    }
    void LoadgrvBan()
    {
        var c = from p in db.BanHocs select p;
        grvBanHoc.DataSource = c;
        grvBanHoc.DataBind();
    }
    void MaTuTangBan()
    {
        var c = from p in db.BanHocs select p.MaBan;
        if (c.Count() == 0)
        {
            lblMaBan.Text = "1";
        }
        if (c.Count() >= 1)
        {
            int max = 0;
            //string s1 = c.ToString();
            int max1 = 0;
            foreach (var con in c)
            {
                max = con;
            }
            max1 = max + 1;
            lblMaBan.Text = max1.ToString();
        }
    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (txtTenLop.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa nhập tên lớp');", true);
        }
        else
        {
            LopHoc lh = new LopHoc();
            lh.MaLop = int.Parse(lblMaLop.Text);
            lh.TenLop = txtTenLop.Text;
            lh.MaKhoi =int.Parse( ddlKhoiHoc.SelectedValue.ToString());
            db.LopHocs.InsertOnSubmit(lh);
            db.SubmitChanges();
            txtTenLop.Text = "";
            LoadgrvLop();
        }
    }
    protected void btnThayDoi_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvLop.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaLop");
        LopHoc lh = db.LopHocs.SingleOrDefault(p=>p.MaLop==int.Parse(Ma.Text));
        lh.MaLop = int.Parse(Ma.Text);
        lh.TenLop = txtTenLop.Text;
        lh.MaKhoi = int.Parse(ddlKhoiHoc.SelectedValue.ToString());
        db.SubmitChanges();

    }
    protected void btnMoi_Click(object sender, EventArgs e)
    {
        txtTenLop.Text = "";
    }
    protected void grvLop_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvLop.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaLop");
        LopHoc lh = db.LopHocs.SingleOrDefault(p => p.MaLop == int.Parse(Ma.Text));
        lblMa.Text = lh.MaKhoi.ToString();
        txtTenLop.Text = lh.TenLop;
        ddlKhoiHoc.SelectedItem.Text = lh.KhoiHoc.TenKhoi;
    }
    void LoadgrvLop()
    {
        var c = from p in db.LopHocs select new {p.MaLop,p.TenLop,p.KhoiHoc.TenKhoi};
        grvLop.DataSource = c;
        grvLop.DataBind();
    }
    void TangMaLop()
    {
        var c = from p in db.LopHocs select p.MaLop;
        if (c.Count() == 0)
        {
            lblMaLop.Text = "1";
        }
        if (c.Count() >= 1)
        {
            int max = 0;
            //string s1 = c.ToString();
            int max1 = 0;
            foreach (var con in c)
            {
                max = con;
            }
            max1 = max + 1;
            lblMaLop.Text = max1.ToString();
        }
    }
    protected void btnNhap_Click(object sender, EventArgs e)
    {
        if (txtTenMon.Text == "" || txtKyHieu.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa nhập tên môn học hoặc ký hiệu');",true);
        }
        else
        {
            MonHoc mh = new MonHoc();
            mh.MaMon = int.Parse(lblMaMon.Text);
            mh.TenMon = txtTenMon.Text;
            mh.KyHieu = txtKyHieu.Text;
            db.MonHocs.InsertOnSubmit(mh);
            db.SubmitChanges();
            LoadgrvMon();
            txtKyHieu.Text = "";
            txtTenMon.Text = "";
        }
    }
    protected void btnFix_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvMon.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaMon");
        MonHoc mh = db.MonHocs.SingleOrDefault(p => p.MaMon == int.Parse(Ma.Text));
        mh.MaMon = int.Parse(Ma.Text);
        mh.TenMon = txtTenMon.Text;
        mh.KyHieu = txtKyHieu.Text;
        db.SubmitChanges();
        LoadgrvMon();
        txtKyHieu.Text = ""; txtTenMon.Text = "";
    }
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {

    }
    protected void grvMon_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvMon.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaMon");
        MonHoc mh = db.MonHocs.SingleOrDefault(p=>p.MaMon==int.Parse(Ma.Text));
        lblMa.Text = mh.MaMon.ToString();
        txtTenMon.Text = mh.TenMon;
        txtKyHieu.Text = mh.KyHieu;
    }
    void LoadgrvMon()
    {
        var c = from p in db.MonHocs select p;
        grvMon.DataSource = c;
        grvMon.DataBind();
    }
    void TangMaMon()
    {
        var c = from p in db.MonHocs select p.MaMon;
        if (c.Count() == 0)
        {
            lblMaMon.Text = "1";
        }
        if (c.Count() >= 1)
        {
            int max = 0;
            //string s1 = c.ToString();
            int max1 = 0;
            foreach (var con in c)
            {
                max = con;
            }
            max1 = max + 1;
            lblMaMon.Text = max1.ToString();
        }
 
    }
    protected void grvBanHoc_SelectedIndexChanged1(object sender, EventArgs e)
    {
        GridViewRow row = grvBanHoc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaBan");
        BanHoc bn = db.BanHocs.SingleOrDefault(p => p.MaBan == int.Parse(Ma.Text));
        txtTenBan.Text = bn.TenBan;
        lblMaBan.Text = bn.MaBan.ToString();
    }
}
